package com.github.tommas.admintpl.security.permission;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.lang.util.StringUtils;

import java.util.Collections;
import java.util.Set;

public class RestrictedPermission implements Permission {
    final private String permission;
    final private Set<String> restrictions;

    public RestrictedPermission(String permission) {
        this(permission, Collections.EMPTY_SET);
    }

    public RestrictedPermission(String permission, Set<String> attributes) {
        this.permission = StringUtils.clean(permission);
        this.restrictions = attributes;
    }

    @Override
    public boolean implies(Permission p) {
        RestrictedPermission ap = (RestrictedPermission) p;
        return this.permission.equals(ap.permission);
    }

    public String getPermission() {
        return permission;
    }

    public Set<String> getRestrictions() {
        return restrictions;
    }

    public boolean hasRestriction() {
        return !restrictions.isEmpty();
    }

    public boolean isRestrictedWith(String restriction) {
        return restrictions.contains(restriction);
    }

    public void addRestrictions(String ...restrictions) {
        Collections.addAll(this.restrictions, restrictions);
    }
}
